package com.qf.ts.platform.redis.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.ts.platform.redis.service.CacheService;
import com.qf.ts.platform.redis.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/cache")
public class CacheController {

    @Autowired
    private CacheService cacheService;

    private Logger logger = LoggerFactory.getLogger(CacheController.class);

    /**
     * 对hash类型进行批量添加
     */
    @PostMapping("/hmset/{key}")
    public boolean hmset(@PathVariable(value = "key") String key, @RequestBody Map<Object, Object> values) throws Exception {
        RedisUtil.chechKeyNull(key);
        return cacheService.hMSet(key, values);
    }

    @PostMapping("/delete/{key}")
    public boolean deleteKey(@PathVariable(value = "key") String key) throws Exception {
        RedisUtil.chechKeyNull(key);
        return cacheService.deleteKey(key);
    }

    /**
     * 降级的配置
     */
    @PostMapping("/set/{key}/{value}/{expireTime}")
    @HystrixCommand(fallbackMethod = "save2RedisFallback")
    public boolean save2Redis(@PathVariable(value = "key") String key, @PathVariable(value = "value") String value, @PathVariable(value = "expireTime") long expireTime) throws Exception {
        RedisUtil.chechKeyNull(key);
        RedisUtil.chechKeyNull(value);
        return cacheService.save2Redis(key, value, expireTime);
    }

    @GetMapping("/get/{key}")
    @HystrixCommand(fallbackMethod = "getFromRedisFallback")
    public String getFromRedis(@PathVariable(value = "key") String key) throws Exception {
        RedisUtil.chechKeyNull(key);
        return cacheService.getFromRedis(key);
    }

    @PostMapping("/expire/{key}/{expireTime}")
    public boolean expire(@PathVariable(value = "key") String key, @PathVariable(value = "expireTime") long expireTime) throws Exception {
        RedisUtil.chechKeyNull(key);
        return cacheService.expire(key, expireTime);
    }

    @GetMapping("/getid/{key}")
    public Long getAutoIncrementId(@PathVariable(value = "key") String key) throws Exception {
        RedisUtil.chechKeyNull(key);
        System.out.println(key);
        return cacheService.getAutoIncrementId(key);
    }

    @GetMapping("/smembers/{key}")
    public Set<Object> sMembers(@PathVariable(value = "key") String key) throws Exception {
        RedisUtil.chechKeyNull(key);
        return cacheService.sMembers(key);
    }

    @PostMapping("/sadd/{key}/{member}")
    public Long sAdd(@PathVariable(value = "key") String key, @PathVariable(value = "member") String member) throws Exception {
        RedisUtil.chechKeyNull(key);
        RedisUtil.chechKeyNull(member);
        return cacheService.sAdd(key, member);
    }

    @PostMapping("/sadds/{key}")
    public Long sAdd(@PathVariable(value = "key") String key, String[] members) throws Exception {
        RedisUtil.chechKeyNull(key);
        return cacheService.sAdd(key, members);
    }

    @PostMapping("/sremove/{key}/{member}")
    public Long sRemove(@PathVariable(value = "key") String key, @PathVariable(value = "member") String member) throws Exception {
        RedisUtil.chechKeyNull(key);
        RedisUtil.chechKeyNull(member);
        return cacheService.sRemove(key, member);
    }

    @PostMapping("/hset/{key}/{field}/{value}")
    public boolean hSet(@PathVariable(value = "key") String key, @PathVariable(value = "field") String field, @PathVariable(value = "value") String value) throws Exception {
        RedisUtil.chechKeyNull(key);
        RedisUtil.chechKeyNull(field);
        RedisUtil.chechKeyNull(value);
        return cacheService.hSet(key, field, value);
    }

    @GetMapping("/hget/{key}/{field}")
    public String hGet(@PathVariable(value = "key") String key, @PathVariable(value = "field") String field) throws Exception {
        RedisUtil.chechKeyNull(key);
        RedisUtil.chechKeyNull(field);
        return cacheService.hGet(key, field);
    }

    @GetMapping("/hgetall/{key}")
    public Map<Object, Object> hGetAll(@PathVariable(value = "key") String key) throws Exception {
        RedisUtil.chechKeyNull(key);
        return cacheService.hGetAll(key);
    }

    @GetMapping("/keys/{pattern}")
    public Set<String> findKeyByPattern(@PathVariable(value = "pattern") String pattern) throws Exception {
        RedisUtil.chechKeyNull(pattern);
        return cacheService.findKeyByPartten(pattern);
    }

    @GetMapping("/increment/{key}/{delta}")
    public Long getAutoIncrementId(@PathVariable(value = "key") String key, @PathVariable(value = "delta") long delta) throws Exception {
        RedisUtil.chechKeyNull(key);
        return cacheService.getAutoIncrementId(key, delta);
    }

    @GetMapping("/hIncrementid/{key}/{field}/{delta}")
    public Long hIncrementId(@PathVariable(value = "key") String key, @PathVariable(value = "field") String field, @PathVariable(value = "delta") long delta) throws Exception {
        RedisUtil.chechKeyNull(key);
        RedisUtil.chechKeyNull(field);
        return cacheService.hIncrementId(key, field, delta);
    }

    @PostMapping("/setnx/{key}/{value}/{expireTime}")
    public boolean setNx(@PathVariable(value = "key") String key, @PathVariable(value = "value") String value, @PathVariable(value = "expireTime") long expireTime) throws Exception {
        RedisUtil.chechKeyNull(key);
        RedisUtil.chechKeyNull(value);
        return cacheService.setNx(key, value, expireTime);
    }

    /**
     * 保存数据到缓存中的降级的方法
     */
    public boolean save2RedisFallback(String key, String value, long expireTime) throws Exception {
        logger.error("save2Redis 出现异常,进行降级,{}:{}", key, value);
        return false;
    }

    @HystrixCommand(fallbackMethod = "getFromRedisFallback")
    public String getFromRedisFallback(String key) throws Exception {
        logger.error("getFromRedis 出现异常,进行降级,{}", key);
        return null;
    }

}
